package cn.study.controller;


import cn.study.entity.StudyProblem;
import cn.study.entity.StudyRole;
import cn.study.service.StudyProblemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 问题表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@Slf4j
@RequestMapping("/study-problem")
public class StudyProblemController {

    @Resource
    StudyProblemService studyProblemService;

    @GetMapping("/list")
    public String list(Model model,@RequestParam(value="pn",defaultValue="1") Integer pn,
                       @RequestParam(value="paginalsize",defaultValue = "5") Integer paginalsize)
    {
        PageHelper.startPage(pn, paginalsize);

        List<StudyProblem> list=studyProblemService.getlist();
        PageInfo pInfo=new PageInfo(list,10);
        model.addAttribute("pInfo",pInfo);
        model.addAttribute("paginalsize",paginalsize);

        return "study_problem";
    }


    @GetMapping("del")
    public String del(Integer id)
    {
        boolean bl=studyProblemService.removeById(id);
        if(bl){
            log.info("删除成功");
        }else {
            log.info("删除失败");
        }
        return "redirect:/study-problem/list";
    }


    @GetMapping("/upd")
    @ResponseBody
    public StudyProblem upd(Integer id)
    {
        StudyProblem studyProblem=studyProblemService.getById(id);


        return studyProblem;
    }
    @PostMapping("/spupd")
    public String spuupd(Model model,StudyProblem problem)
    {
        studyProblemService.updateById(problem);

        return "redirect:/study-problem/list";
    }
}

